package com.qqt.csr.route.service;

import com.qqt.csr.common.utils.JsonUtil;
import com.qqt.csr.route.dto.resp.RouteResultDTO;
import com.qqt.csr.route.entity.RouteTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;


/**
 * mq生产者
 */
@Slf4j
@Service
public class RouteMqProducerService {
    /**
     * 路由任务topic
     */
    public static final String ROUTE_DISTRIBUTE_TASK_TOPIC = "distribute_route_task_topic";
    /**
     * 路由阶段通知topic
     */
    public static final String ROUTE_DISTRIBUTE_RESULT_TOPIC = "distribute_route_result_topic";

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送路由分发到mq
     *
     * @param routeTask
     */
    public void sendRouteTask(RouteTask routeTask) {
        SendResult sendResult = rocketMQTemplate.syncSend(ROUTE_DISTRIBUTE_TASK_TOPIC, MessageBuilder.withPayload(JsonUtil.toJson(routeTask)).build());
        log.info("【sendRouteTask】sendResult={}", JsonUtil.toJson(sendResult));
    }

    /**
     * 发送路由分发结果到mq
     * tip: 推送路由结果到MQ，给到客服工作台消费，以做后续逻辑以及统一通过IM回复访客
     *
     * @param routeResultDTO
     */
    public void sendRouteResult(RouteResultDTO routeResultDTO) {
        SendResult sendResult = rocketMQTemplate.syncSend(ROUTE_DISTRIBUTE_RESULT_TOPIC, MessageBuilder.withPayload(JsonUtil.toJson(routeResultDTO)).build());
        log.info("【sendRouteResult】sendResult={}", JsonUtil.toJson(sendResult));
    }
}
